<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/24
 * Time: 13:28
 */

namespace app\admin\controller;

use app\admin\model\Carousel as C;
class Carousel extends User
{

    public function index(){
        $m=new C();
        $img= $m
            ->alias('c')
            ->join('partner p','c.partner_id=p.id',"left")
            ->order('c.img_sort desc')
            ->field("c.*,p.name as partner_name")
            ->paginate(20);
        $this->assign('img',$img);
        return $this->fetch();
    }

    public function addimg(){
        $d=new C();
        if (request()->post()){
            $dat=request()->post();
//            dump($dat);

            if ($dat['Association']=='goods'){
                $goods= db('goods')->where('goods_status',1)->field('goods_id as id,goods_name as name')->find();
                $datas=$goods;
            }else if($dat['Association']=='poi'){
//            poi
                $poi= db('poi')->where('deleted',0)->field('id,name')->find();
                $datas=$poi;
            }else if($dat['Association']=='city'){
//            poi
                $s['deleted']='0';
                $s['groupid']=['NEQ',0];
                $s['id']=$dat['img_url'];
                $city= db('city')->where($s)->field('id,name')->find();
                $datas=$city;
            }

            $url=[
                'id'=>$dat['img_url'],
                'types'=>$dat['Association'],
                'name'=>$datas['name']
            ];
            $url=json_encode($url);

            $data["img_name"] =$dat['img_name'];
            $data["img_src"] = $dat['img_src'];
            $data["img_url"] =$url;
            $data["show_hidden"] =$dat['show_hidden'];
            $data["img_sort"] =$dat['img_sort'];
            $data["terminal"] =$dat['terminal'];
            $da=$d->insert($data);
            if ($da==true){
                return $this->success('上传成功');
            }else{
                return $this->error('上传失败');
            }
        }
        return $this->fetch();
    }


    public function edit(){
        $d=new C();
        $id='';
        if (request()->post()){
            $dat=request()->post();
            $id=$dat['id'];
            unset($dat['id']);
            $datas = [];
            if ($dat['Association']=='goods'){
                $goods= db('goods')->where('goods_status',1)->field('goods_id as id,goods_name as name')->find();
                $datas=$goods;
            }else if($dat['Association']=='poi'){
                $poi= db('poi')->where('deleted',0)->field('id,name')->find();
                $datas=$poi;
            }else if($dat['Association']=='city'){
                $s['deleted']='0';
                $s['groupid']=['NEQ',0];
                $s['id']=$dat['img_url'];
                $city= db('city')->where($s)->field('id,name')->find();
                $datas=$city;
            }else if($dat['Association']=='article'){
                $s['id']=$dat['img_url'];
                $article= db('article')->where($s)->field('id,title as name')->find();
                $datas=$article;
            }else if($dat['Association']=='active'){
                $active_url=$dat['active_url'];
                $datas['name']=$active_url;
            }

            $url=[
                'id'=>$dat['img_url'],
                'types'=>$dat['Association'],
                'name'=>$datas['name']
            ];
            $url=json_encode($url);
            $data["img_name"] =$dat['img_name'];
            if (!empty($dat['img_src'])){
                $data["img_src"] = $dat['img_src'];
            }
            $data["img_url"] =$url;
            $data["partner_id"] =$dat['partner_id'];
            $data["show_hidden"] =$dat['show_hidden'];
            $data["img_sort"] =$dat['img_sort'];
            $data["terminal"] =$dat['terminal'];
            $da=$d ->where('id',$id)->Update($data);
            if ($da==true){
                return $this->success('编辑成功');
            }else{
                return $this->error('编辑失败');
            }
        }else{
            $id=input('id');
            $da=  $d->where('id',$id)->find();
            $data=[];
            if (!empty($da['img_url'])){
                $da['img_url']=json_decode( $da['img_url']);
                if ($da['img_url']->types=='goods'){
                    $goods= db('goods')->where('goods_status',1)->field('goods_id as id,goods_name as name')->select();
                    $data=$goods;
                }else if($da['img_url']->types=='poi'){
                    $poi= db('poi')->where('deleted',0)->field('id,name')->select();
                    $data=$poi;
                }else if($da['img_url']->types=='city'){
                    $city= db('city')->where('deleted',0)->where('groupid','NEQ',0)->field('id,name')->select();
                    $data=$city;
                }else if($da['img_url']->types=='article'){
                    $article= db('article')->field('id,title as name')->order("id desc")->select();
                    $data=$article;
                }
            }

            $partner = db("partner")->select();

            $this->assign('data',$da);
            $this->assign('partner',$partner);
            $this->assign('goods',$data);
            return $this->fetch();
        }
    }

    public function shelves(){
        $d=new C();
        $data =request()->request();
        if (!empty($data)){
            $show_hidden='';
            if ($data['show_hidden']=='0'){
                $show_hidden=1;
            }else  if ($data['show_hidden']=='1'){
                $show_hidden=0;
            }
            $s= $d->where('id',$data['id'])->Update(
                ['show_hidden'=>$show_hidden]
            );
            if ($s==true){
                return $this->success('状态切换成功');
            }else{
                return $this->error('切换失败');
            }
        }else{
            return $this->error('参数错误');
        }
    }


    public function Association(){
        $var= input('val');
        $data=[];
        if ($var =='goods'){
            $goods= db('goods')->where('goods_status',1)->field('goods_id as id,goods_name as name')->select();
            $data=$goods;
        }else if($var=='poi'){
            $poi= db('poi')->where('deleted',0)->field('id,name')->select();
            $data=$poi;
        }else if($var=='city'){
            $city= db('city')->where('deleted',0)->where('groupid','NEQ',0)->field('id,name')->select();
            $data=$city;
        }else if($var=='article'){
            $article= db('article')->field('id,title as name')->order("id desc")->select();
            $data=$article;
        }

        return json_encode($data);
    }


    public function delete()
    {
        if($this->request->isAjax()) {
            $d = new C();
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false ==$d->where('id',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                return $this->success('删除成功');
            }
        }
    }


}